package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeReFundManage;
import com.kehutong.charge.entity.ChargeRefundConfig;
import com.kehutong.charge.service.ChargeReFundService;
import com.kehutong.charge.service.ChargeRefundConfigService;
import com.kehutong.charge.util.enums.EnableStateEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.util.time.TimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @program: kht
 * @description: 退款管理
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/refund")
public class ChargeRefundController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeRefundController.class);

    @Inject
    DeptService deptService;
    @Inject
    ChargeReFundService chargeReFundService;
    @Inject
    ChargeRefundConfigService chargeRefundConfigService;



    @ReqMapping("/manage/page")
    @HasPermission("pay:charge:refund:view")
    public Object pageManage(JSONObject jsonObject) {
        return chargeReFundService.findPage(jsonObject);
    }


    @ReqMapping("/manage/page/export")
    @HasPermission("pay:charge:refund:view")
    public void pageManageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        chargeReFundService.pageManageExport(req,resp,jsonObject);
    }


    @ReqMapping("/apply/page/export")
    @HasPermission("pay:charge:refund:view")
    public void pageApplyExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        chargeReFundService.pageApplyExport(req,resp,jsonObject);
    }


    @ReqMapping("/manage/list")
    @HasPermission("pay:charge:refund:view")
    public Object listManage(JSONObject jsonObject) {
        return chargeReFundService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:refund:edit")
    @ReqMapping("/manage/save")
    public Object saveManage(ChargeReFundManage entity, Token token,JSONObject jsonObject) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setApplyTime(TimeUtil.timestamp());
        String reason = jsonObject.getString("reason");
        if(Objects.isNull(reason)){
            return Result.error(123,"退款说明必填！");
        }

        Object obj = 0;
        JSONArray jsonArray = jsonObject.getJSONArray("orderNoList");
        for (int i = 0; i < jsonArray.size(); i++) {
            String orderNo = jsonArray.getString(i);
            entity.setOrderNo(orderNo);
            obj = chargeReFundService.save(entity);
        }
        return obj;
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:refund:edit")
    @ReqMapping("/manage/update")
    public Object updateManage(ChargeReFundManage entity,Token token) throws Exception {
        if(!Arrays.asList(entity.getApplyUserNo().split(",")).contains(token.getUuid())){
            return Result.error(123,"不是指定的审核人！");
        }
        entity.setUpdateName(token.getUsername());
        entity.setUpdateNo(token.getUuid());
        return Result.success(chargeReFundService.update(entity));
    }

    @HasPermission("pay:charge:refund:edit")
    @ReqMapping("/manage/delete")
    public Object deleteManage(ChargeReFundManage entity) {

        return chargeReFundService.deleteById(entity.getId());
    }

    @ReqMapping("/manage/get")
    public Object getManage(ChargeReFundManage entity) {

        return Result.success(entity);
    }


    @ReqMapping("/page")
    @HasPermission("pay:charge:refund:config:view")
    public Object page(JSONObject jsonObject) {
        return chargeRefundConfigService.findPage(jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:refund:config:view")
    public Object list(JSONObject jsonObject) {
        return chargeRefundConfigService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:refund:config:edit")
    @ReqMapping("/save")
    public Object save(ChargeRefundConfig entity,JSONObject jsonObject, Token token) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        String str = jsonObject.getString("approveMethod");
        if(str.equals("0")){
            entity.setApproveMethod(EnableStateEnum.UNUSED);
        }else{
            entity.setApproveMethod(EnableStateEnum.ENABLE);
        }

        return chargeRefundConfigService.save(entity);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:refund:config:edit")
    @ReqMapping("/update")
    public Object update(ChargeRefundConfig entity,JSONObject jsonObject) throws Exception {

        String str = jsonObject.getString("approveMethod");
        if(str.equals("0")){
            entity.setApproveMethod(EnableStateEnum.UNUSED);
        }else{
            entity.setApproveMethod(EnableStateEnum.ENABLE);
        }

        return chargeRefundConfigService.update(entity);
    }


    @HasPermission("pay:charge:refund:config:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeRefundConfig entity) {

        return chargeRefundConfigService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ChargeRefundConfig entity) {

        return Result.success(entity);
    }


    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:refund:config:edit")
    public Object setStatus(ChargeRefundConfig entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        chargeRefundConfigService.update(entity);
        return Result.success();
    }

}
